<?php

namespace app\index\controller;

use think\Controller;
use think\Request;
use think\Db;
class Game extends Controller
{
	public function index()
	{	
		$list = Db::name('Game')->select();
		return view('Game/index',[
			'list' => $list,
			]);
	}
	public function add()
	{
		return view('Game/addGame');
	}
	public function cadd(Request $request)
	{
		$p = $request->post();
		if (!is_dir('static/uploads'.DS.date('Ymd',time()))) {
			mkdir('static/uploads/'.date('Ymd',time()));
		} 
	    // 获取表单上传文件 例如上传了001.jpg
	    $file = request()->file('game');
	    // 移动到框架应用根目录/public/uploads/ 目录下
	    $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
	    if($info){
	        // 成功上传后 获取上传信息
	        // 输出 jpg
	        // echo $info->getExtension();
	        // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
	        // echo $info->getSaveName();
	        // 输出 42a79759f284b767dfcb2a0197904287.jpg
	        // echo $info->getFilename(); 
	    }else{
	        // 上传失败获取错误信息
        echo $file->getError();
    }

    	$data = ['gname' => $p['ljname'], 'gfile' => $info->getSaveName()];
		$list = Db::name('Game')->insert($data);
		if ($list) {
			return $this->success('添加成功',url('index/Game/index'));
		}
	}
	public function shang($id)
	{	
		$list = Db::name('game')->where('gid',$id)->update(['status'=>'1']);
		if ($list > 0) {
			return $this->success('更改成功',url('index/Game/index'));
		} else {
			return $this->seeor('更改失败');
		}
	}
	public function xia($id)
	{
		$ll = Db::name('game')->field('gid')->select();
		foreach ($ll as $key => $value) {
			$ll = $value['gid'];
			$mu = Db::name('game')->where('gid',$ll)->update(['status'=>'1']);
		}
		$list = Db::name('game')->where('gid',$id)->update(['status'=>'0']);
		if ($list > 0) {
			return $this->success('更改成功',url('index/Game/index'));
		} else {
			return $this->seeor('更改失败');
		}
	}
	public function delect($id)
	{
		$list = Db::name('game')->delete($id);
		if ($list > 0) {
			return $this->success('删除成功',url('index/Game/index'));
		} else {
			return $this->seeor('删除失败');
		}
	}

	public function gamegoog($id)
	{
		$list = Db::name('Game')->field('gfile')->where('gid',$id)->find();
		$list = $list['gfile'];
		// var_dump($list);die;
		return view('uploads/'.$list);
	}
}